]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/char/virtio_console.c
virtio: console: Reference counting portdev structs is not needed
[mirror_ubuntu-jammy-kernel.git] / drivers / char / virtio_console.c
CommitLineData
a23ea924
RR
1/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
17634ba2 3 * Copyright (C) 2009, 2010 Red Hat, Inc.
31610434
RR
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
fb08bd27 19#include <linux/cdev.h>
d99393ef 20#include <linux/debugfs.h>
fb08bd27 21#include <linux/device.h>
31610434 22#include <linux/err.h>
2030fa49 23#include <linux/fs.h>
31610434 24#include <linux/init.h>
38edf58d 25#include <linux/list.h>
2030fa49
AS
26#include <linux/poll.h>
27#include <linux/sched.h>
5a0e3ad6 28#include <linux/slab.h>
38edf58d 29#include <linux/spinlock.h>
31610434
RR
30#include <linux/virtio.h>
31#include <linux/virtio_console.h>
2030fa49 32#include <linux/wait.h>
17634ba2 33#include <linux/workqueue.h>
31610434
RR
34#include "hvc_console.h"
35
38edf58d
AS
36/*
37 * This is a global struct for storing common data for all the devices
38 * this driver handles.
39 *
40 * Mainly, it has a linked list for all the consoles in one place so
41 * that callbacks from hvc for get_chars(), put_chars() work properly
42 * across multiple devices and multiple ports per device.
43 */
44struct ports_driver_data {
fb08bd27
AS
45 /* Used for registering chardevs */
46 struct class *class;
47
d99393ef
AS
48 /* Used for exporting per-port information to debugfs */
49 struct dentry *debugfs_dir;
50
6bdf2afd
AS
51 /* List of all the devices we're handling */
52 struct list_head portdevs;
53
fb08bd27
AS
54 /* Number of devices this driver is handling */
55 unsigned int index;
56
d8a02bd5
RR
57 /*
58 * This is used to keep track of the number of hvc consoles
59 * spawned by this driver. This number is given as the first
60 * argument to hvc_alloc(). To correctly map an initial
61 * console spawned via hvc_instantiate to the console being
62 * hooked up via hvc_alloc, we need to pass the same vtermno.
63 *
64 * We also just assume the first console being initialised was
65 * the first one that got used as the initial console.
66 */
67 unsigned int next_vtermno;
68
38edf58d
AS
69 /* All the console devices handled by this driver */
70 struct list_head consoles;
71};
72static struct ports_driver_data pdrvdata;
73
74DEFINE_SPINLOCK(pdrvdata_lock);
75
4f23c573
AS
76/* This struct holds information that's relevant only for console ports */
77struct console {
78 /* We'll place all consoles in a list in the pdrvdata struct */
79 struct list_head list;
80
81 /* The hvc device associated with this console port */
82 struct hvc_struct *hvc;
83
9778829c
AS
84 /* The size of the console */
85 struct winsize ws;
86
4f23c573
AS
87 /*
88 * This number identifies the number that we used to register
89 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
90 * number passed on by the hvc callbacks to us to
91 * differentiate between the other console ports handled by
92 * this driver
93 */
94 u32 vtermno;
95};
96
fdb9a054
AS
97struct port_buffer {
98 char *buf;
99
100 /* size of the buffer in *buf above */
101 size_t size;
102
103 /* used length of the buffer */
104 size_t len;
105 /* offset in the buf from which to consume data */
106 size_t offset;
107};
108
17634ba2
AS
109/*
110 * This is a per-device struct that stores data common to all the
111 * ports for that device (vdev->priv).
112 */
113struct ports_device {
6bdf2afd
AS
114 /* Next portdev in the list, head is in the pdrvdata struct */
115 struct list_head list;
116
17634ba2
AS
117 /*
118 * Workqueue handlers where we process deferred work after
119 * notification
120 */
121 struct work_struct control_work;
122
123 struct list_head ports;
124
125 /* To protect the list of ports */
126 spinlock_t ports_lock;
127
128 /* To protect the vq operations for the control channel */
129 spinlock_t cvq_lock;
130
131 /* The current config space is stored here */
b99fa815 132 struct virtio_console_config config;
17634ba2
AS
133
134 /* The virtio device we're associated with */
135 struct virtio_device *vdev;
136
137 /*
138 * A couple of virtqueues for the control channel: one for
139 * guest->host transfers, one for host->guest transfers
140 */
141 struct virtqueue *c_ivq, *c_ovq;
142
143 /* Array of per-port IO virtqueues */
144 struct virtqueue **in_vqs, **out_vqs;
fb08bd27
AS
145
146 /* Used for numbering devices for sysfs and debugfs */
147 unsigned int drv_index;
148
149 /* Major number for this device. Ports will be created as minors. */
150 int chr_major;
17634ba2
AS
151};
152
1c85bf35 153/* This struct holds the per-port data */
21206ede 154struct port {
17634ba2
AS
155 /* Next port in the list, head is in the ports_device */
156 struct list_head list;
157
1c85bf35
AS
158 /* Pointer to the parent virtio_console device */
159 struct ports_device *portdev;
fdb9a054
AS
160
161 /* The current buffer from which data has to be fed to readers */
162 struct port_buffer *inbuf;
21206ede 163
203baab8
AS
164 /*
165 * To protect the operations on the in_vq associated with this
166 * port. Has to be a spinlock because it can be called from
167 * interrupt context (get_char()).
168 */
169 spinlock_t inbuf_lock;
170
cdfadfc1
AS
171 /* Protect the operations on the out_vq. */
172 spinlock_t outvq_lock;
173
1c85bf35
AS
174 /* The IO vqs for this port */
175 struct virtqueue *in_vq, *out_vq;
176
d99393ef
AS
177 /* File in the debugfs directory that exposes this port's information */
178 struct dentry *debugfs_file;
179
4f23c573
AS
180 /*
181 * The entries in this struct will be valid if this port is
182 * hooked up to an hvc console
183 */
184 struct console cons;
17634ba2 185
fb08bd27 186 /* Each port associates with a separate char device */
d22a6989 187 struct cdev *cdev;
fb08bd27
AS
188 struct device *dev;
189
b353a6b8
AS
190 /* Reference-counting to handle port hot-unplugs and file operations */
191 struct kref kref;
192
2030fa49
AS
193 /* A waitqueue for poll() or blocking read operations */
194 wait_queue_head_t waitqueue;
195
431edb8a
AS
196 /* The 'name' of the port that we expose via sysfs properties */
197 char *name;
198
17634ba2
AS
199 /* The 'id' to identify the port with the Host */
200 u32 id;
2030fa49 201
cdfadfc1
AS
202 bool outvq_full;
203
2030fa49
AS
204 /* Is the host device open */
205 bool host_connected;
3c7969cc
AS
206
207 /* We should allow only one process to open a port */
208 bool guest_connected;
21206ede 209};
31610434 210
971f3390
RR
211/* This is the very early arch-specified put chars function. */
212static int (*early_put_chars)(u32, const char *, int);
213
38edf58d
AS
214static struct port *find_port_by_vtermno(u32 vtermno)
215{
216 struct port *port;
4f23c573 217 struct console *cons;
38edf58d
AS
218 unsigned long flags;
219
220 spin_lock_irqsave(&pdrvdata_lock, flags);
4f23c573
AS
221 list_for_each_entry(cons, &pdrvdata.consoles, list) {
222 if (cons->vtermno == vtermno) {
223 port = container_of(cons, struct port, cons);
38edf58d 224 goto out;
4f23c573 225 }
38edf58d
AS
226 }
227 port = NULL;
228out:
229 spin_unlock_irqrestore(&pdrvdata_lock, flags);
230 return port;
231}
232
04950cdf
AS
233static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
234 dev_t dev)
235{
236 struct port *port;
237 unsigned long flags;
238
239 spin_lock_irqsave(&portdev->ports_lock, flags);
240 list_for_each_entry(port, &portdev->ports, list)
d22a6989 241 if (port->cdev->dev == dev)
04950cdf
AS
242 goto out;
243 port = NULL;
244out:
245 spin_unlock_irqrestore(&portdev->ports_lock, flags);
246
247 return port;
248}
249
250static struct port *find_port_by_devt(dev_t dev)
251{
252 struct ports_device *portdev;
253 struct port *port;
254 unsigned long flags;
255
256 spin_lock_irqsave(&pdrvdata_lock, flags);
257 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
258 port = find_port_by_devt_in_portdev(portdev, dev);
259 if (port)
260 goto out;
261 }
262 port = NULL;
263out:
264 spin_unlock_irqrestore(&pdrvdata_lock, flags);
265 return port;
266}
267
17634ba2
AS
268static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
269{
270 struct port *port;
271 unsigned long flags;
272
273 spin_lock_irqsave(&portdev->ports_lock, flags);
274 list_for_each_entry(port, &portdev->ports, list)
275 if (port->id == id)
276 goto out;
277 port = NULL;
278out:
279 spin_unlock_irqrestore(&portdev->ports_lock, flags);
280
281 return port;
282}
283
203baab8
AS
284static struct port *find_port_by_vq(struct ports_device *portdev,
285 struct virtqueue *vq)
286{
287 struct port *port;
203baab8
AS
288 unsigned long flags;
289
17634ba2
AS
290 spin_lock_irqsave(&portdev->ports_lock, flags);
291 list_for_each_entry(port, &portdev->ports, list)
203baab8
AS
292 if (port->in_vq == vq || port->out_vq == vq)
293 goto out;
203baab8
AS
294 port = NULL;
295out:
17634ba2 296 spin_unlock_irqrestore(&portdev->ports_lock, flags);
203baab8
AS
297 return port;
298}
299
17634ba2
AS
300static bool is_console_port(struct port *port)
301{
302 if (port->cons.hvc)
303 return true;
304 return false;
305}
306
307static inline bool use_multiport(struct ports_device *portdev)
308{
309 /*
310 * This condition can be true when put_chars is called from
311 * early_init
312 */
313 if (!portdev->vdev)
314 return 0;
315 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
316}
317
fdb9a054
AS
318static void free_buf(struct port_buffer *buf)
319{
320 kfree(buf->buf);
321 kfree(buf);
322}
323
324static struct port_buffer *alloc_buf(size_t buf_size)
325{
326 struct port_buffer *buf;
327
328 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
329 if (!buf)
330 goto fail;
331 buf->buf = kzalloc(buf_size, GFP_KERNEL);
332 if (!buf->buf)
333 goto free_buf;
334 buf->len = 0;
335 buf->offset = 0;
336 buf->size = buf_size;
337 return buf;
338
339free_buf:
340 kfree(buf);
341fail:
342 return NULL;
343}
344
a3cde449
AS
345/* Callers should take appropriate locks */
346static void *get_inbuf(struct port *port)
347{
348 struct port_buffer *buf;
349 struct virtqueue *vq;
350 unsigned int len;
351
352 vq = port->in_vq;
505b0451 353 buf = virtqueue_get_buf(vq, &len);
a3cde449
AS
354 if (buf) {
355 buf->len = len;
356 buf->offset = 0;
357 }
358 return buf;
359}
360
e27b5198
AS
361/*
362 * Create a scatter-gather list representing our input buffer and put
363 * it in the queue.
364 *
365 * Callers should take appropriate locks.
366 */
203baab8 367static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
e27b5198
AS
368{
369 struct scatterlist sg[1];
203baab8 370 int ret;
1c85bf35 371
e27b5198
AS
372 sg_init_one(sg, buf->buf, buf->size);
373
505b0451
MT
374 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
375 virtqueue_kick(vq);
203baab8
AS
376 return ret;
377}
378
88f251ac
AS
379/* Discard any unread data this port has. Callers lockers. */
380static void discard_port_data(struct port *port)
381{
382 struct port_buffer *buf;
383 struct virtqueue *vq;
384 unsigned int len;
d6933561 385 int ret;
88f251ac
AS
386
387 vq = port->in_vq;
388 if (port->inbuf)
389 buf = port->inbuf;
390 else
505b0451 391 buf = virtqueue_get_buf(vq, &len);
88f251ac 392
d6933561
AS
393 ret = 0;
394 while (buf) {
395 if (add_inbuf(vq, buf) < 0) {
396 ret++;
397 free_buf(buf);
398 }
505b0451 399 buf = virtqueue_get_buf(vq, &len);
88f251ac 400 }
88f251ac 401 port->inbuf = NULL;
d6933561
AS
402 if (ret)
403 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
404 ret);
88f251ac
AS
405}
406
203baab8
AS
407static bool port_has_data(struct port *port)
408{
409 unsigned long flags;
410 bool ret;
411
203baab8 412 spin_lock_irqsave(&port->inbuf_lock, flags);
d6933561 413 if (port->inbuf) {
203baab8 414 ret = true;
d6933561
AS
415 goto out;
416 }
417 port->inbuf = get_inbuf(port);
418 if (port->inbuf) {
419 ret = true;
420 goto out;
421 }
422 ret = false;
423out:
203baab8 424 spin_unlock_irqrestore(&port->inbuf_lock, flags);
203baab8
AS
425 return ret;
426}
427
3425e706
AS
428static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
429 unsigned int event, unsigned int value)
17634ba2
AS
430{
431 struct scatterlist sg[1];
432 struct virtio_console_control cpkt;
433 struct virtqueue *vq;
604b2ad7 434 unsigned int len;
17634ba2 435
3425e706 436 if (!use_multiport(portdev))
17634ba2
AS
437 return 0;
438
3425e706 439 cpkt.id = port_id;
17634ba2
AS
440 cpkt.event = event;
441 cpkt.value = value;
442
3425e706 443 vq = portdev->c_ovq;
17634ba2
AS
444
445 sg_init_one(sg, &cpkt, sizeof(cpkt));
505b0451
MT
446 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
447 virtqueue_kick(vq);
448 while (!virtqueue_get_buf(vq, &len))
17634ba2
AS
449 cpu_relax();
450 }
451 return 0;
452}
453
3425e706
AS
454static ssize_t send_control_msg(struct port *port, unsigned int event,
455 unsigned int value)
456{
84ec06c5
AS
457 /* Did the port get unplugged before userspace closed it? */
458 if (port->portdev)
459 return __send_control_msg(port->portdev, port->id, event, value);
460 return 0;
3425e706
AS
461}
462
cdfadfc1
AS
463/* Callers must take the port->outvq_lock */
464static void reclaim_consumed_buffers(struct port *port)
465{
466 void *buf;
467 unsigned int len;
468
469 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
470 kfree(buf);
471 port->outvq_full = false;
472 }
473}
474
475static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
476 bool nonblock)
f997f00b
AS
477{
478 struct scatterlist sg[1];
479 struct virtqueue *out_vq;
480 ssize_t ret;
cdfadfc1 481 unsigned long flags;
f997f00b
AS
482 unsigned int len;
483
484 out_vq = port->out_vq;
485
cdfadfc1
AS
486 spin_lock_irqsave(&port->outvq_lock, flags);
487
488 reclaim_consumed_buffers(port);
489
f997f00b 490 sg_init_one(sg, in_buf, in_count);
505b0451 491 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
f997f00b
AS
492
493 /* Tell Host to go! */
505b0451 494 virtqueue_kick(out_vq);
f997f00b
AS
495
496 if (ret < 0) {
9ff4cfab 497 in_count = 0;
cdfadfc1 498 goto done;
f997f00b
AS
499 }
500
cdfadfc1
AS
501 if (ret == 0)
502 port->outvq_full = true;
503
504 if (nonblock)
505 goto done;
506
507 /*
508 * Wait till the host acknowledges it pushed out the data we
531295e6
AS
509 * sent. This is done for data from the hvc_console; the tty
510 * operations are performed with spinlocks held so we can't
511 * sleep here. An alternative would be to copy the data to a
512 * buffer and relax the spinning requirement. The downside is
513 * we need to kmalloc a GFP_ATOMIC buffer each time the
514 * console driver writes something out.
cdfadfc1 515 */
505b0451 516 while (!virtqueue_get_buf(out_vq, &len))
f997f00b 517 cpu_relax();
cdfadfc1
AS
518done:
519 spin_unlock_irqrestore(&port->outvq_lock, flags);
520 /*
521 * We're expected to return the amount of data we wrote -- all
522 * of it
523 */
9ff4cfab 524 return in_count;
f997f00b
AS
525}
526
203baab8
AS
527/*
528 * Give out the data that's requested from the buffer that we have
529 * queued up.
530 */
b766ceed
AS
531static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
532 bool to_user)
203baab8
AS
533{
534 struct port_buffer *buf;
535 unsigned long flags;
536
537 if (!out_count || !port_has_data(port))
538 return 0;
539
540 buf = port->inbuf;
b766ceed 541 out_count = min(out_count, buf->len - buf->offset);
203baab8 542
b766ceed
AS
543 if (to_user) {
544 ssize_t ret;
545
546 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
547 if (ret)
548 return -EFAULT;
549 } else {
550 memcpy(out_buf, buf->buf + buf->offset, out_count);
551 }
203baab8 552
203baab8
AS
553 buf->offset += out_count;
554
555 if (buf->offset == buf->len) {
556 /*
557 * We're done using all the data in this buffer.
558 * Re-queue so that the Host can send us more data.
559 */
560 spin_lock_irqsave(&port->inbuf_lock, flags);
561 port->inbuf = NULL;
562
563 if (add_inbuf(port->in_vq, buf) < 0)
fb08bd27 564 dev_warn(port->dev, "failed add_buf\n");
203baab8
AS
565
566 spin_unlock_irqrestore(&port->inbuf_lock, flags);
567 }
b766ceed 568 /* Return the number of bytes actually copied */
203baab8 569 return out_count;
e27b5198
AS
570}
571
2030fa49 572/* The condition that must be true for polling to end */
60caacd3 573static bool will_read_block(struct port *port)
2030fa49 574{
3709ea7a
AS
575 if (!port->guest_connected) {
576 /* Port got hot-unplugged. Let's exit. */
577 return false;
578 }
60caacd3 579 return !port_has_data(port) && port->host_connected;
2030fa49
AS
580}
581
cdfadfc1
AS
582static bool will_write_block(struct port *port)
583{
584 bool ret;
585
60e5e0b8
AS
586 if (!port->guest_connected) {
587 /* Port got hot-unplugged. Let's exit. */
588 return false;
589 }
cdfadfc1
AS
590 if (!port->host_connected)
591 return true;
592
593 spin_lock_irq(&port->outvq_lock);
594 /*
595 * Check if the Host has consumed any buffers since we last
596 * sent data (this is only applicable for nonblocking ports).
597 */
598 reclaim_consumed_buffers(port);
599 ret = port->outvq_full;
600 spin_unlock_irq(&port->outvq_lock);
601
602 return ret;
603}
604
2030fa49
AS
605static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
606 size_t count, loff_t *offp)
607{
608 struct port *port;
609 ssize_t ret;
610
611 port = filp->private_data;
612
613 if (!port_has_data(port)) {
614 /*
615 * If nothing's connected on the host just return 0 in
616 * case of list_empty; this tells the userspace app
617 * that there's no connection
618 */
619 if (!port->host_connected)
620 return 0;
621 if (filp->f_flags & O_NONBLOCK)
622 return -EAGAIN;
623
624 ret = wait_event_interruptible(port->waitqueue,
60caacd3 625 !will_read_block(port));
2030fa49
AS
626 if (ret < 0)
627 return ret;
628 }
b3dddb9e
AS
629 /* Port got hot-unplugged. */
630 if (!port->guest_connected)
631 return -ENODEV;
2030fa49
AS
632 /*
633 * We could've received a disconnection message while we were
634 * waiting for more data.
635 *
636 * This check is not clubbed in the if() statement above as we
637 * might receive some data as well as the host could get
638 * disconnected after we got woken up from our wait. So we
639 * really want to give off whatever data we have and only then
640 * check for host_connected.
641 */
642 if (!port_has_data(port) && !port->host_connected)
643 return 0;
644
645 return fill_readbuf(port, ubuf, count, true);
646}
647
648static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
649 size_t count, loff_t *offp)
650{
651 struct port *port;
652 char *buf;
653 ssize_t ret;
cdfadfc1 654 bool nonblock;
2030fa49 655
65745422
AS
656 /* Userspace could be out to fool us */
657 if (!count)
658 return 0;
659
2030fa49
AS
660 port = filp->private_data;
661
cdfadfc1
AS
662 nonblock = filp->f_flags & O_NONBLOCK;
663
664 if (will_write_block(port)) {
665 if (nonblock)
666 return -EAGAIN;
667
668 ret = wait_event_interruptible(port->waitqueue,
669 !will_write_block(port));
670 if (ret < 0)
671 return ret;
672 }
f4028119
AS
673 /* Port got hot-unplugged. */
674 if (!port->guest_connected)
675 return -ENODEV;
cdfadfc1 676
2030fa49
AS
677 count = min((size_t)(32 * 1024), count);
678
679 buf = kmalloc(count, GFP_KERNEL);
680 if (!buf)
681 return -ENOMEM;
682
683 ret = copy_from_user(buf, ubuf, count);
684 if (ret) {
685 ret = -EFAULT;
686 goto free_buf;
687 }
688
531295e6
AS
689 /*
690 * We now ask send_buf() to not spin for generic ports -- we
691 * can re-use the same code path that non-blocking file
692 * descriptors take for blocking file descriptors since the
693 * wait is already done and we're certain the write will go
694 * through to the host.
695 */
696 nonblock = true;
cdfadfc1
AS
697 ret = send_buf(port, buf, count, nonblock);
698
699 if (nonblock && ret > 0)
700 goto out;
701
2030fa49
AS
702free_buf:
703 kfree(buf);
cdfadfc1 704out:
2030fa49
AS
705 return ret;
706}
707
708static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
709{
710 struct port *port;
711 unsigned int ret;
712
713 port = filp->private_data;
714 poll_wait(filp, &port->waitqueue, wait);
715
8529a504
AS
716 if (!port->guest_connected) {
717 /* Port got unplugged */
718 return POLLHUP;
719 }
2030fa49 720 ret = 0;
6df7aadc 721 if (!will_read_block(port))
2030fa49 722 ret |= POLLIN | POLLRDNORM;
cdfadfc1 723 if (!will_write_block(port))
2030fa49
AS
724 ret |= POLLOUT;
725 if (!port->host_connected)
726 ret |= POLLHUP;
727
728 return ret;
729}
730
b353a6b8
AS
731static void remove_port(struct kref *kref);
732
2030fa49
AS
733static int port_fops_release(struct inode *inode, struct file *filp)
734{
735 struct port *port;
736
737 port = filp->private_data;
738
739 /* Notify host of port being closed */
740 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
741
88f251ac 742 spin_lock_irq(&port->inbuf_lock);
3c7969cc
AS
743 port->guest_connected = false;
744
88f251ac
AS
745 discard_port_data(port);
746
747 spin_unlock_irq(&port->inbuf_lock);
748
cdfadfc1
AS
749 spin_lock_irq(&port->outvq_lock);
750 reclaim_consumed_buffers(port);
751 spin_unlock_irq(&port->outvq_lock);
752
b353a6b8
AS
753 /*
754 * Locks aren't necessary here as a port can't be opened after
755 * unplug, and if a port isn't unplugged, a kref would already
756 * exist for the port. Plus, taking ports_lock here would
757 * create a dependency on other locks taken by functions
758 * inside remove_port if we're the last holder of the port,
759 * creating many problems.
760 */
761 kref_put(&port->kref, remove_port);
762
2030fa49
AS
763 return 0;
764}
765
766static int port_fops_open(struct inode *inode, struct file *filp)
767{
768 struct cdev *cdev = inode->i_cdev;
769 struct port *port;
8ad37e83 770 int ret;
2030fa49 771
04950cdf 772 port = find_port_by_devt(cdev->dev);
2030fa49
AS
773 filp->private_data = port;
774
b353a6b8
AS
775 /* Prevent against a port getting hot-unplugged at the same time */
776 spin_lock_irq(&port->portdev->ports_lock);
777 kref_get(&port->kref);
778 spin_unlock_irq(&port->portdev->ports_lock);
779
2030fa49
AS
780 /*
781 * Don't allow opening of console port devices -- that's done
782 * via /dev/hvc
783 */
8ad37e83
AS
784 if (is_console_port(port)) {
785 ret = -ENXIO;
786 goto out;
787 }
2030fa49 788
3c7969cc
AS
789 /* Allow only one process to open a particular port at a time */
790 spin_lock_irq(&port->inbuf_lock);
791 if (port->guest_connected) {
792 spin_unlock_irq(&port->inbuf_lock);
8ad37e83
AS
793 ret = -EMFILE;
794 goto out;
3c7969cc
AS
795 }
796
797 port->guest_connected = true;
798 spin_unlock_irq(&port->inbuf_lock);
799
cdfadfc1
AS
800 spin_lock_irq(&port->outvq_lock);
801 /*
802 * There might be a chance that we missed reclaiming a few
803 * buffers in the window of the port getting previously closed
804 * and opening now.
805 */
806 reclaim_consumed_buffers(port);
807 spin_unlock_irq(&port->outvq_lock);
808
2030fa49
AS
809 /* Notify host of port being opened */
810 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
811
812 return 0;
8ad37e83 813out:
b353a6b8 814 kref_put(&port->kref, remove_port);
8ad37e83 815 return ret;
2030fa49
AS
816}
817
818/*
819 * The file operations that we support: programs in the guest can open
820 * a console device, read from it, write to it, poll for data and
821 * close it. The devices are at
822 * /dev/vport<device number>p<port number>
823 */
824static const struct file_operations port_fops = {
825 .owner = THIS_MODULE,
826 .open = port_fops_open,
827 .read = port_fops_read,
828 .write = port_fops_write,
829 .poll = port_fops_poll,
830 .release = port_fops_release,
831};
832
a23ea924
RR
833/*
834 * The put_chars() callback is pretty straightforward.
31610434 835 *
a23ea924
RR
836 * We turn the characters into a scatter-gather list, add it to the
837 * output queue and then kick the Host. Then we sit here waiting for
838 * it to finish: inefficient in theory, but in practice
839 * implementations will do it immediately (lguest's Launcher does).
840 */
31610434
RR
841static int put_chars(u32 vtermno, const char *buf, int count)
842{
21206ede 843 struct port *port;
38edf58d 844
162a689a
FD
845 if (unlikely(early_put_chars))
846 return early_put_chars(vtermno, buf, count);
847
38edf58d
AS
848 port = find_port_by_vtermno(vtermno);
849 if (!port)
6dc69f97 850 return -EPIPE;
31610434 851
cdfadfc1 852 return send_buf(port, (void *)buf, count, false);
31610434
RR
853}
854
a23ea924
RR
855/*
856 * get_chars() is the callback from the hvc_console infrastructure
857 * when an interrupt is received.
31610434 858 *
203baab8
AS
859 * We call out to fill_readbuf that gets us the required data from the
860 * buffers that are queued up.
a23ea924 861 */
31610434
RR
862static int get_chars(u32 vtermno, char *buf, int count)
863{
21206ede
RR
864 struct port *port;
865
6dc69f97
AS
866 /* If we've not set up the port yet, we have no input to give. */
867 if (unlikely(early_put_chars))
868 return 0;
869
38edf58d
AS
870 port = find_port_by_vtermno(vtermno);
871 if (!port)
6dc69f97 872 return -EPIPE;
21206ede 873
31610434 874 /* If we don't have an input queue yet, we can't get input. */
21206ede 875 BUG_ON(!port->in_vq);
31610434 876
b766ceed 877 return fill_readbuf(port, buf, count, false);
31610434 878}
31610434 879
cb06e367 880static void resize_console(struct port *port)
c2983458 881{
cb06e367 882 struct virtio_device *vdev;
c2983458 883
2de16a49 884 /* The port could have been hot-unplugged */
9778829c 885 if (!port || !is_console_port(port))
2de16a49
AS
886 return;
887
cb06e367 888 vdev = port->portdev->vdev;
9778829c
AS
889 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
890 hvc_resize(port->cons.hvc, port->cons.ws);
c2983458
CB
891}
892
38edf58d 893/* We set the configuration at this point, since we now have a tty */
91fcad19
CB
894static int notifier_add_vio(struct hvc_struct *hp, int data)
895{
38edf58d
AS
896 struct port *port;
897
898 port = find_port_by_vtermno(hp->vtermno);
899 if (!port)
900 return -EINVAL;
901
91fcad19 902 hp->irq_requested = 1;
cb06e367 903 resize_console(port);
c2983458 904
91fcad19
CB
905 return 0;
906}
907
908static void notifier_del_vio(struct hvc_struct *hp, int data)
909{
910 hp->irq_requested = 0;
911}
912
17634ba2 913/* The operations for console ports. */
1dff3996 914static const struct hv_ops hv_ops = {
971f3390
RR
915 .get_chars = get_chars,
916 .put_chars = put_chars,
917 .notifier_add = notifier_add_vio,
918 .notifier_del = notifier_del_vio,
919 .notifier_hangup = notifier_del_vio,
920};
921
922/*
923 * Console drivers are initialized very early so boot messages can go
924 * out, so we do things slightly differently from the generic virtio
925 * initialization of the net and block drivers.
926 *
927 * At this stage, the console is output-only. It's too early to set
928 * up a virtqueue, so we let the drivers do some boutique early-output
929 * thing.
930 */
931int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
932{
933 early_put_chars = put_chars;
934 return hvc_instantiate(0, 0, &hv_ops);
935}
936
17634ba2 937int init_port_console(struct port *port)
cfa6d379
AS
938{
939 int ret;
940
941 /*
942 * The Host's telling us this port is a console port. Hook it
943 * up with an hvc console.
944 *
945 * To set up and manage our virtual console, we call
946 * hvc_alloc().
947 *
948 * The first argument of hvc_alloc() is the virtual console
949 * number. The second argument is the parameter for the
950 * notification mechanism (like irq number). We currently
951 * leave this as zero, virtqueues have implicit notifications.
952 *
953 * The third argument is a "struct hv_ops" containing the
954 * put_chars() get_chars(), notifier_add() and notifier_del()
955 * pointers. The final argument is the output buffer size: we
956 * can do any size, so we put PAGE_SIZE here.
957 */
958 port->cons.vtermno = pdrvdata.next_vtermno;
959
960 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
961 if (IS_ERR(port->cons.hvc)) {
962 ret = PTR_ERR(port->cons.hvc);
298add72
AS
963 dev_err(port->dev,
964 "error %d allocating hvc for port\n", ret);
cfa6d379
AS
965 port->cons.hvc = NULL;
966 return ret;
967 }
968 spin_lock_irq(&pdrvdata_lock);
969 pdrvdata.next_vtermno++;
970 list_add_tail(&port->cons.list, &pdrvdata.consoles);
971 spin_unlock_irq(&pdrvdata_lock);
3c7969cc 972 port->guest_connected = true;
cfa6d379 973
1d05160b
AS
974 /*
975 * Start using the new console output if this is the first
976 * console to come up.
977 */
978 if (early_put_chars)
979 early_put_chars = NULL;
980
2030fa49
AS
981 /* Notify host of port being opened */
982 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
983
cfa6d379
AS
984 return 0;
985}
986
431edb8a
AS
987static ssize_t show_port_name(struct device *dev,
988 struct device_attribute *attr, char *buffer)
989{
990 struct port *port;
991
992 port = dev_get_drvdata(dev);
993
994 return sprintf(buffer, "%s\n", port->name);
995}
996
997static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
998
999static struct attribute *port_sysfs_entries[] = {
1000 &dev_attr_name.attr,
1001 NULL
1002};
1003
1004static struct attribute_group port_attribute_group = {
1005 .name = NULL, /* put in device directory */
1006 .attrs = port_sysfs_entries,
1007};
1008
d99393ef
AS
1009static int debugfs_open(struct inode *inode, struct file *filp)
1010{
1011 filp->private_data = inode->i_private;
1012 return 0;
1013}
1014
1015static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1016 size_t count, loff_t *offp)
1017{
1018 struct port *port;
1019 char *buf;
1020 ssize_t ret, out_offset, out_count;
1021
1022 out_count = 1024;
1023 buf = kmalloc(out_count, GFP_KERNEL);
1024 if (!buf)
1025 return -ENOMEM;
1026
1027 port = filp->private_data;
1028 out_offset = 0;
1029 out_offset += snprintf(buf + out_offset, out_count,
1030 "name: %s\n", port->name ? port->name : "");
1031 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1032 "guest_connected: %d\n", port->guest_connected);
1033 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1034 "host_connected: %d\n", port->host_connected);
cdfadfc1
AS
1035 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1036 "outvq_full: %d\n", port->outvq_full);
d99393ef
AS
1037 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1038 "is_console: %s\n",
1039 is_console_port(port) ? "yes" : "no");
1040 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1041 "console_vtermno: %u\n", port->cons.vtermno);
1042
1043 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1044 kfree(buf);
1045 return ret;
1046}
1047
1048static const struct file_operations port_debugfs_ops = {
1049 .owner = THIS_MODULE,
1050 .open = debugfs_open,
1051 .read = debugfs_read,
1052};
1053
9778829c
AS
1054static void set_console_size(struct port *port, u16 rows, u16 cols)
1055{
1056 if (!port || !is_console_port(port))
1057 return;
1058
1059 port->cons.ws.ws_row = rows;
1060 port->cons.ws.ws_col = cols;
1061}
1062
c446f8fc
AS
1063static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1064{
1065 struct port_buffer *buf;
1066 unsigned int nr_added_bufs;
1067 int ret;
1068
1069 nr_added_bufs = 0;
1070 do {
1071 buf = alloc_buf(PAGE_SIZE);
1072 if (!buf)
1073 break;
1074
1075 spin_lock_irq(lock);
1076 ret = add_inbuf(vq, buf);
1077 if (ret < 0) {
1078 spin_unlock_irq(lock);
1079 free_buf(buf);
1080 break;
1081 }
1082 nr_added_bufs++;
1083 spin_unlock_irq(lock);
1084 } while (ret > 0);
1085
1086 return nr_added_bufs;
1087}
1088
1089static int add_port(struct ports_device *portdev, u32 id)
1090{
1091 char debugfs_name[16];
1092 struct port *port;
1093 struct port_buffer *buf;
1094 dev_t devt;
1095 unsigned int nr_added_bufs;
1096 int err;
1097
1098 port = kmalloc(sizeof(*port), GFP_KERNEL);
1099 if (!port) {
1100 err = -ENOMEM;
1101 goto fail;
1102 }
b353a6b8 1103 kref_init(&port->kref);
c446f8fc
AS
1104
1105 port->portdev = portdev;
1106 port->id = id;
1107
1108 port->name = NULL;
1109 port->inbuf = NULL;
1110 port->cons.hvc = NULL;
1111
9778829c
AS
1112 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1113
c446f8fc
AS
1114 port->host_connected = port->guest_connected = false;
1115
cdfadfc1
AS
1116 port->outvq_full = false;
1117
c446f8fc
AS
1118 port->in_vq = portdev->in_vqs[port->id];
1119 port->out_vq = portdev->out_vqs[port->id];
1120
d22a6989
AS
1121 port->cdev = cdev_alloc();
1122 if (!port->cdev) {
1123 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1124 err = -ENOMEM;
1125 goto free_port;
1126 }
1127 port->cdev->ops = &port_fops;
c446f8fc
AS
1128
1129 devt = MKDEV(portdev->chr_major, id);
d22a6989 1130 err = cdev_add(port->cdev, devt, 1);
c446f8fc
AS
1131 if (err < 0) {
1132 dev_err(&port->portdev->vdev->dev,
1133 "Error %d adding cdev for port %u\n", err, id);
d22a6989 1134 goto free_cdev;
c446f8fc
AS
1135 }
1136 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1137 devt, port, "vport%up%u",
1138 port->portdev->drv_index, id);
1139 if (IS_ERR(port->dev)) {
1140 err = PTR_ERR(port->dev);
1141 dev_err(&port->portdev->vdev->dev,
1142 "Error %d creating device for port %u\n",
1143 err, id);
1144 goto free_cdev;
1145 }
1146
1147 spin_lock_init(&port->inbuf_lock);
cdfadfc1 1148 spin_lock_init(&port->outvq_lock);
c446f8fc
AS
1149 init_waitqueue_head(&port->waitqueue);
1150
1151 /* Fill the in_vq with buffers so the host can send us data. */
1152 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1153 if (!nr_added_bufs) {
1154 dev_err(port->dev, "Error allocating inbufs\n");
1155 err = -ENOMEM;
1156 goto free_device;
1157 }
1158
1159 /*
1160 * If we're not using multiport support, this has to be a console port
1161 */
1162 if (!use_multiport(port->portdev)) {
1163 err = init_port_console(port);
1164 if (err)
1165 goto free_inbufs;
1166 }
1167
1168 spin_lock_irq(&portdev->ports_lock);
1169 list_add_tail(&port->list, &port->portdev->ports);
1170 spin_unlock_irq(&portdev->ports_lock);
1171
1172 /*
1173 * Tell the Host we're set so that it can send us various
1174 * configuration parameters for this port (eg, port name,
1175 * caching, whether this is a console port, etc.)
1176 */
1177 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1178
1179 if (pdrvdata.debugfs_dir) {
1180 /*
1181 * Finally, create the debugfs file that we can use to
1182 * inspect a port's state at any time
1183 */
1184 sprintf(debugfs_name, "vport%up%u",
1185 port->portdev->drv_index, id);
1186 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1187 pdrvdata.debugfs_dir,
1188 port,
1189 &port_debugfs_ops);
1190 }
1191 return 0;
1192
1193free_inbufs:
1194 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1195 free_buf(buf);
1196free_device:
1197 device_destroy(pdrvdata.class, port->dev->devt);
1198free_cdev:
d22a6989 1199 cdev_del(port->cdev);
c446f8fc
AS
1200free_port:
1201 kfree(port);
1202fail:
1203 /* The host might want to notify management sw about port add failure */
0643e4c6 1204 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
c446f8fc
AS
1205 return err;
1206}
1207
b353a6b8
AS
1208/* No users remain, remove all port-specific data. */
1209static void remove_port(struct kref *kref)
1210{
1211 struct port *port;
1212
1213 port = container_of(kref, struct port, kref);
1214
1215 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1216 device_destroy(pdrvdata.class, port->dev->devt);
1217 cdev_del(port->cdev);
1218
1219 kfree(port->name);
1220
1221 debugfs_remove(port->debugfs_file);
1222
1223 kfree(port);
1224}
1225
1226/*
1227 * Port got unplugged. Remove port from portdev's list and drop the
1228 * kref reference. If no userspace has this port opened, it will
1229 * result in immediate removal the port.
1230 */
1231static void unplug_port(struct port *port)
1f7aa42d 1232{
a9cdd485
AS
1233 struct port_buffer *buf;
1234
b353a6b8
AS
1235 spin_lock_irq(&port->portdev->ports_lock);
1236 list_del(&port->list);
1237 spin_unlock_irq(&port->portdev->ports_lock);
1238
0047634d
AS
1239 if (port->guest_connected) {
1240 port->guest_connected = false;
1241 port->host_connected = false;
1242 wake_up_interruptible(&port->waitqueue);
0047634d
AS
1243 }
1244
1f7aa42d
AS
1245 if (is_console_port(port)) {
1246 spin_lock_irq(&pdrvdata_lock);
1247 list_del(&port->cons.list);
1248 spin_unlock_irq(&pdrvdata_lock);
69eb9a9f
AS
1249#if 0
1250 /*
1251 * hvc_remove() not called as removing one hvc port
1252 * results in other hvc ports getting frozen.
1253 *
1254 * Once this is resolved in hvc, this functionality
1255 * will be enabled. Till that is done, the -EPIPE
1256 * return from get_chars() above will help
1257 * hvc_console.c to clean up on ports we remove here.
1258 */
1f7aa42d 1259 hvc_remove(port->cons.hvc);
69eb9a9f 1260#endif
1f7aa42d 1261 }
1f7aa42d 1262
a9cdd485 1263 /* Remove unused data this port might have received. */
1f7aa42d 1264 discard_port_data(port);
a9cdd485 1265
cdfadfc1
AS
1266 reclaim_consumed_buffers(port);
1267
a9cdd485 1268 /* Remove buffers we queued up for the Host to send us data in. */
505b0451 1269 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
a9cdd485
AS
1270 free_buf(buf);
1271
b353a6b8
AS
1272 /*
1273 * We should just assume the device itself has gone off --
1274 * else a close on an open port later will try to send out a
1275 * control message.
1276 */
1277 port->portdev = NULL;
d99393ef 1278
b353a6b8
AS
1279 /*
1280 * Locks around here are not necessary - a port can't be
1281 * opened after we removed the port struct from ports_list
1282 * above.
1283 */
1284 kref_put(&port->kref, remove_port);
1f7aa42d
AS
1285}
1286
17634ba2
AS
1287/* Any private messages that the Host and Guest want to share */
1288static void handle_control_message(struct ports_device *portdev,
1289 struct port_buffer *buf)
1290{
1291 struct virtio_console_control *cpkt;
1292 struct port *port;
431edb8a
AS
1293 size_t name_size;
1294 int err;
17634ba2
AS
1295
1296 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1297
1298 port = find_port_by_id(portdev, cpkt->id);
f909f850 1299 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
17634ba2
AS
1300 /* No valid header at start of buffer. Drop it. */
1301 dev_dbg(&portdev->vdev->dev,
1302 "Invalid index %u in control packet\n", cpkt->id);
1303 return;
1304 }
1305
1306 switch (cpkt->event) {
f909f850
AS
1307 case VIRTIO_CONSOLE_PORT_ADD:
1308 if (port) {
1d05160b
AS
1309 dev_dbg(&portdev->vdev->dev,
1310 "Port %u already added\n", port->id);
f909f850
AS
1311 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1312 break;
1313 }
1314 if (cpkt->id >= portdev->config.max_nr_ports) {
1315 dev_warn(&portdev->vdev->dev,
1316 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1317 cpkt->id, portdev->config.max_nr_ports - 1);
1318 break;
1319 }
1320 add_port(portdev, cpkt->id);
1321 break;
1322 case VIRTIO_CONSOLE_PORT_REMOVE:
b353a6b8 1323 unplug_port(port);
f909f850 1324 break;
17634ba2
AS
1325 case VIRTIO_CONSOLE_CONSOLE_PORT:
1326 if (!cpkt->value)
1327 break;
1328 if (is_console_port(port))
1329 break;
1330
1331 init_port_console(port);
1332 /*
1333 * Could remove the port here in case init fails - but
1334 * have to notify the host first.
1335 */
1336 break;
8345adbf
AS
1337 case VIRTIO_CONSOLE_RESIZE: {
1338 struct {
1339 __u16 rows;
1340 __u16 cols;
1341 } size;
1342
17634ba2
AS
1343 if (!is_console_port(port))
1344 break;
8345adbf
AS
1345
1346 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1347 sizeof(size));
1348 set_console_size(port, size.rows, size.cols);
1349
17634ba2
AS
1350 port->cons.hvc->irq_requested = 1;
1351 resize_console(port);
1352 break;
8345adbf 1353 }
2030fa49
AS
1354 case VIRTIO_CONSOLE_PORT_OPEN:
1355 port->host_connected = cpkt->value;
1356 wake_up_interruptible(&port->waitqueue);
cdfadfc1
AS
1357 /*
1358 * If the host port got closed and the host had any
1359 * unconsumed buffers, we'll be able to reclaim them
1360 * now.
1361 */
1362 spin_lock_irq(&port->outvq_lock);
1363 reclaim_consumed_buffers(port);
1364 spin_unlock_irq(&port->outvq_lock);
2030fa49 1365 break;
431edb8a
AS
1366 case VIRTIO_CONSOLE_PORT_NAME:
1367 /*
1368 * Skip the size of the header and the cpkt to get the size
1369 * of the name that was sent
1370 */
1371 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1372
1373 port->name = kmalloc(name_size, GFP_KERNEL);
1374 if (!port->name) {
1375 dev_err(port->dev,
1376 "Not enough space to store port name\n");
1377 break;
1378 }
1379 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1380 name_size - 1);
1381 port->name[name_size - 1] = 0;
1382
1383 /*
1384 * Since we only have one sysfs attribute, 'name',
1385 * create it only if we have a name for the port.
1386 */
1387 err = sysfs_create_group(&port->dev->kobj,
1388 &port_attribute_group);
ec64213c 1389 if (err) {
431edb8a
AS
1390 dev_err(port->dev,
1391 "Error %d creating sysfs device attributes\n",
1392 err);
ec64213c
AS
1393 } else {
1394 /*
1395 * Generate a udev event so that appropriate
1396 * symlinks can be created based on udev
1397 * rules.
1398 */
1399 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1400 }
431edb8a 1401 break;
17634ba2
AS
1402 }
1403}
1404
1405static void control_work_handler(struct work_struct *work)
1406{
1407 struct ports_device *portdev;
1408 struct virtqueue *vq;
1409 struct port_buffer *buf;
1410 unsigned int len;
1411
1412 portdev = container_of(work, struct ports_device, control_work);
1413 vq = portdev->c_ivq;
1414
1415 spin_lock(&portdev->cvq_lock);
505b0451 1416 while ((buf = virtqueue_get_buf(vq, &len))) {
17634ba2
AS
1417 spin_unlock(&portdev->cvq_lock);
1418
1419 buf->len = len;
1420 buf->offset = 0;
1421
1422 handle_control_message(portdev, buf);
1423
1424 spin_lock(&portdev->cvq_lock);
1425 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1426 dev_warn(&portdev->vdev->dev,
1427 "Error adding buffer to queue\n");
1428 free_buf(buf);
1429 }
1430 }
1431 spin_unlock(&portdev->cvq_lock);
1432}
1433
1434static void in_intr(struct virtqueue *vq)
1435{
1436 struct port *port;
1437 unsigned long flags;
1438
1439 port = find_port_by_vq(vq->vdev->priv, vq);
1440 if (!port)
1441 return;
1442
1443 spin_lock_irqsave(&port->inbuf_lock, flags);
d6933561
AS
1444 if (!port->inbuf)
1445 port->inbuf = get_inbuf(port);
17634ba2 1446
88f251ac
AS
1447 /*
1448 * Don't queue up data when port is closed. This condition
1449 * can be reached when a console port is not yet connected (no
1450 * tty is spawned) and the host sends out data to console
1451 * ports. For generic serial ports, the host won't
1452 * (shouldn't) send data till the guest is connected.
1453 */
1454 if (!port->guest_connected)
1455 discard_port_data(port);
1456
17634ba2
AS
1457 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1458
2030fa49
AS
1459 wake_up_interruptible(&port->waitqueue);
1460
17634ba2
AS
1461 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1462 hvc_kick();
1463}
1464
1465static void control_intr(struct virtqueue *vq)
1466{
1467 struct ports_device *portdev;
1468
1469 portdev = vq->vdev->priv;
1470 schedule_work(&portdev->control_work);
1471}
1472
7f5d810d
AS
1473static void config_intr(struct virtio_device *vdev)
1474{
1475 struct ports_device *portdev;
1476
1477 portdev = vdev->priv;
99f905f8 1478
4038f5b7 1479 if (!use_multiport(portdev)) {
9778829c
AS
1480 struct port *port;
1481 u16 rows, cols;
1482
1483 vdev->config->get(vdev,
1484 offsetof(struct virtio_console_config, cols),
1485 &cols, sizeof(u16));
1486 vdev->config->get(vdev,
1487 offsetof(struct virtio_console_config, rows),
1488 &rows, sizeof(u16));
1489
1490 port = find_port_by_id(portdev, 0);
1491 set_console_size(port, rows, cols);
1492
4038f5b7
AS
1493 /*
1494 * We'll use this way of resizing only for legacy
1495 * support. For newer userspace
1496 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1497 * to indicate console size changes so that it can be
1498 * done per-port.
1499 */
9778829c 1500 resize_console(port);
4038f5b7 1501 }
7f5d810d
AS
1502}
1503
2658a79a
AS
1504static int init_vqs(struct ports_device *portdev)
1505{
1506 vq_callback_t **io_callbacks;
1507 char **io_names;
1508 struct virtqueue **vqs;
17634ba2 1509 u32 i, j, nr_ports, nr_queues;
2658a79a
AS
1510 int err;
1511
17634ba2
AS
1512 nr_ports = portdev->config.max_nr_ports;
1513 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
2658a79a
AS
1514
1515 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1516 if (!vqs) {
1517 err = -ENOMEM;
1518 goto fail;
1519 }
1520 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1521 if (!io_callbacks) {
1522 err = -ENOMEM;
1523 goto free_vqs;
1524 }
1525 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1526 if (!io_names) {
1527 err = -ENOMEM;
1528 goto free_callbacks;
1529 }
1530 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1531 GFP_KERNEL);
1532 if (!portdev->in_vqs) {
1533 err = -ENOMEM;
1534 goto free_names;
1535 }
1536 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1537 GFP_KERNEL);
1538 if (!portdev->out_vqs) {
1539 err = -ENOMEM;
1540 goto free_invqs;
1541 }
1542
17634ba2
AS
1543 /*
1544 * For backward compat (newer host but older guest), the host
1545 * spawns a console port first and also inits the vqs for port
1546 * 0 before others.
1547 */
1548 j = 0;
1549 io_callbacks[j] = in_intr;
1550 io_callbacks[j + 1] = NULL;
1551 io_names[j] = "input";
1552 io_names[j + 1] = "output";
1553 j += 2;
1554
1555 if (use_multiport(portdev)) {
1556 io_callbacks[j] = control_intr;
1557 io_callbacks[j + 1] = NULL;
1558 io_names[j] = "control-i";
1559 io_names[j + 1] = "control-o";
1560
1561 for (i = 1; i < nr_ports; i++) {
1562 j += 2;
1563 io_callbacks[j] = in_intr;
1564 io_callbacks[j + 1] = NULL;
1565 io_names[j] = "input";
1566 io_names[j + 1] = "output";
1567 }
1568 }
2658a79a
AS
1569 /* Find the queues. */
1570 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1571 io_callbacks,
1572 (const char **)io_names);
1573 if (err)
1574 goto free_outvqs;
1575
17634ba2 1576 j = 0;
2658a79a
AS
1577 portdev->in_vqs[0] = vqs[0];
1578 portdev->out_vqs[0] = vqs[1];
17634ba2
AS
1579 j += 2;
1580 if (use_multiport(portdev)) {
1581 portdev->c_ivq = vqs[j];
1582 portdev->c_ovq = vqs[j + 1];
1583
1584 for (i = 1; i < nr_ports; i++) {
1585 j += 2;
1586 portdev->in_vqs[i] = vqs[j];
1587 portdev->out_vqs[i] = vqs[j + 1];
1588 }
1589 }
2658a79a
AS
1590 kfree(io_callbacks);
1591 kfree(io_names);
1592 kfree(vqs);
1593
1594 return 0;
1595
1596free_names:
1597 kfree(io_names);
1598free_callbacks:
1599 kfree(io_callbacks);
1600free_outvqs:
1601 kfree(portdev->out_vqs);
1602free_invqs:
1603 kfree(portdev->in_vqs);
1604free_vqs:
1605 kfree(vqs);
1606fail:
1607 return err;
1608}
1609
fb08bd27
AS
1610static const struct file_operations portdev_fops = {
1611 .owner = THIS_MODULE,
1612};
1613
1c85bf35
AS
1614/*
1615 * Once we're further in boot, we get probed like any other virtio
1616 * device.
17634ba2
AS
1617 *
1618 * If the host also supports multiple console ports, we check the
1619 * config space to see how many ports the host has spawned. We
1620 * initialize each port found.
1c85bf35
AS
1621 */
1622static int __devinit virtcons_probe(struct virtio_device *vdev)
1623{
1c85bf35
AS
1624 struct ports_device *portdev;
1625 int err;
17634ba2 1626 bool multiport;
1c85bf35
AS
1627
1628 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1629 if (!portdev) {
1630 err = -ENOMEM;
1631 goto fail;
1632 }
1633
1634 /* Attach this portdev to this virtio_device, and vice-versa. */
1635 portdev->vdev = vdev;
1636 vdev->priv = portdev;
1637
fb08bd27
AS
1638 spin_lock_irq(&pdrvdata_lock);
1639 portdev->drv_index = pdrvdata.index++;
1640 spin_unlock_irq(&pdrvdata_lock);
1641
1642 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1643 &portdev_fops);
1644 if (portdev->chr_major < 0) {
1645 dev_err(&vdev->dev,
1646 "Error %d registering chrdev for device %u\n",
1647 portdev->chr_major, portdev->drv_index);
1648 err = portdev->chr_major;
1649 goto free;
1650 }
1651
17634ba2 1652 multiport = false;
17634ba2
AS
1653 portdev->config.max_nr_ports = 1;
1654 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1655 multiport = true;
1656 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1657
b99fa815
AS
1658 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1659 max_nr_ports),
17634ba2
AS
1660 &portdev->config.max_nr_ports,
1661 sizeof(portdev->config.max_nr_ports));
17634ba2
AS
1662 }
1663
1664 /* Let the Host know we support multiple ports.*/
1665 vdev->config->finalize_features(vdev);
1666
2658a79a
AS
1667 err = init_vqs(portdev);
1668 if (err < 0) {
1669 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
fb08bd27 1670 goto free_chrdev;
2658a79a 1671 }
1c85bf35 1672
17634ba2
AS
1673 spin_lock_init(&portdev->ports_lock);
1674 INIT_LIST_HEAD(&portdev->ports);
1675
1676 if (multiport) {
335a64a5
AS
1677 unsigned int nr_added_bufs;
1678
17634ba2
AS
1679 spin_lock_init(&portdev->cvq_lock);
1680 INIT_WORK(&portdev->control_work, &control_work_handler);
1681
335a64a5
AS
1682 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1683 if (!nr_added_bufs) {
22a29eac
AS
1684 dev_err(&vdev->dev,
1685 "Error allocating buffers for control queue\n");
1686 err = -ENOMEM;
1687 goto free_vqs;
1688 }
1d05160b
AS
1689 } else {
1690 /*
1691 * For backward compatibility: Create a console port
1692 * if we're running on older host.
1693 */
1694 add_port(portdev, 0);
17634ba2
AS
1695 }
1696
6bdf2afd
AS
1697 spin_lock_irq(&pdrvdata_lock);
1698 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1699 spin_unlock_irq(&pdrvdata_lock);
1700
f909f850
AS
1701 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1702 VIRTIO_CONSOLE_DEVICE_READY, 1);
31610434
RR
1703 return 0;
1704
22a29eac 1705free_vqs:
0643e4c6
JL
1706 /* The host might want to notify mgmt sw about device add failure */
1707 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1708 VIRTIO_CONSOLE_DEVICE_READY, 0);
22a29eac
AS
1709 vdev->config->del_vqs(vdev);
1710 kfree(portdev->in_vqs);
1711 kfree(portdev->out_vqs);
fb08bd27
AS
1712free_chrdev:
1713 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
31610434 1714free:
1c85bf35 1715 kfree(portdev);
31610434
RR
1716fail:
1717 return err;
1718}
1719
7177876f
AS
1720static void virtcons_remove(struct virtio_device *vdev)
1721{
1722 struct ports_device *portdev;
1723 struct port *port, *port2;
7177876f
AS
1724
1725 portdev = vdev->priv;
1726
6bdf2afd
AS
1727 spin_lock_irq(&pdrvdata_lock);
1728 list_del(&portdev->list);
1729 spin_unlock_irq(&pdrvdata_lock);
1730
02238959
AS
1731 /* Disable interrupts for vqs */
1732 vdev->config->reset(vdev);
1733 /* Finish up work that's lined up */
7177876f 1734 cancel_work_sync(&portdev->control_work);
7177876f
AS
1735
1736 list_for_each_entry_safe(port, port2, &portdev->ports, list)
b353a6b8 1737 unplug_port(port);
7177876f
AS
1738
1739 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1740
e062013c
AS
1741 /*
1742 * When yanking out a device, we immediately lose the
1743 * (device-side) queues. So there's no point in keeping the
1744 * guest side around till we drop our final reference. This
1745 * also means that any ports which are in an open state will
1746 * have to just stop using the port, as the vqs are going
1747 * away.
1748 */
96eb872b
AS
1749 if (use_multiport(portdev)) {
1750 struct port_buffer *buf;
1751 unsigned int len;
7177876f 1752
96eb872b
AS
1753 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1754 free_buf(buf);
1755
1756 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1757 free_buf(buf);
1758 }
7177876f
AS
1759
1760 vdev->config->del_vqs(vdev);
1761 kfree(portdev->in_vqs);
1762 kfree(portdev->out_vqs);
1763
1764 kfree(portdev);
1765}
1766
31610434
RR
1767static struct virtio_device_id id_table[] = {
1768 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1769 { 0 },
1770};
1771
c2983458
CB
1772static unsigned int features[] = {
1773 VIRTIO_CONSOLE_F_SIZE,
b99fa815 1774 VIRTIO_CONSOLE_F_MULTIPORT,
c2983458
CB
1775};
1776
31610434 1777static struct virtio_driver virtio_console = {
c2983458
CB
1778 .feature_table = features,
1779 .feature_table_size = ARRAY_SIZE(features),
31610434
RR
1780 .driver.name = KBUILD_MODNAME,
1781 .driver.owner = THIS_MODULE,
1782 .id_table = id_table,
1783 .probe = virtcons_probe,
7177876f 1784 .remove = virtcons_remove,
7f5d810d 1785 .config_changed = config_intr,
31610434
RR
1786};
1787
1788static int __init init(void)
1789{
fb08bd27
AS
1790 int err;
1791
1792 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1793 if (IS_ERR(pdrvdata.class)) {
1794 err = PTR_ERR(pdrvdata.class);
1795 pr_err("Error %d creating virtio-ports class\n", err);
1796 return err;
1797 }
d99393ef
AS
1798
1799 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1800 if (!pdrvdata.debugfs_dir) {
1801 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1802 PTR_ERR(pdrvdata.debugfs_dir));
1803 }
38edf58d 1804 INIT_LIST_HEAD(&pdrvdata.consoles);
6bdf2afd 1805 INIT_LIST_HEAD(&pdrvdata.portdevs);
38edf58d 1806
31610434
RR
1807 return register_virtio_driver(&virtio_console);
1808}
7177876f
AS
1809
1810static void __exit fini(void)
1811{
1812 unregister_virtio_driver(&virtio_console);
1813
1814 class_destroy(pdrvdata.class);
1815 if (pdrvdata.debugfs_dir)
1816 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1817}
31610434 1818module_init(init);
7177876f 1819module_exit(fini);
31610434
RR
1820
1821MODULE_DEVICE_TABLE(virtio, id_table);
1822MODULE_DESCRIPTION("Virtio console driver");
1823MODULE_LICENSE("GPL");