virtio: console: Register with sysfs and create a 'name' attribute for ports
[deliverable/linux.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
AS
19#include <linux/cdev.h>
20#include <linux/device.h>
31610434 21#include <linux/err.h>
2030fa49 22#include <linux/fs.h>
31610434 23#include <linux/init.h>
38edf58d 24#include <linux/list.h>
2030fa49
AS
25#include <linux/poll.h>
26#include <linux/sched.h>
38edf58d 27#include <linux/spinlock.h>
31610434
RR
28#include <linux/virtio.h>
29#include <linux/virtio_console.h>
2030fa49 30#include <linux/wait.h>
17634ba2 31#include <linux/workqueue.h>
31610434
RR
32#include "hvc_console.h"
33
38edf58d
AS
34/*
35 * This is a global struct for storing common data for all the devices
36 * this driver handles.
37 *
38 * Mainly, it has a linked list for all the consoles in one place so
39 * that callbacks from hvc for get_chars(), put_chars() work properly
40 * across multiple devices and multiple ports per device.
41 */
42struct ports_driver_data {
fb08bd27
AS
43 /* Used for registering chardevs */
44 struct class *class;
45
46 /* Number of devices this driver is handling */
47 unsigned int index;
48
d8a02bd5
RR
49 /*
50 * This is used to keep track of the number of hvc consoles
51 * spawned by this driver. This number is given as the first
52 * argument to hvc_alloc(). To correctly map an initial
53 * console spawned via hvc_instantiate to the console being
54 * hooked up via hvc_alloc, we need to pass the same vtermno.
55 *
56 * We also just assume the first console being initialised was
57 * the first one that got used as the initial console.
58 */
59 unsigned int next_vtermno;
60
38edf58d
AS
61 /* All the console devices handled by this driver */
62 struct list_head consoles;
63};
64static struct ports_driver_data pdrvdata;
65
66DEFINE_SPINLOCK(pdrvdata_lock);
67
4f23c573
AS
68/* This struct holds information that's relevant only for console ports */
69struct console {
70 /* We'll place all consoles in a list in the pdrvdata struct */
71 struct list_head list;
72
73 /* The hvc device associated with this console port */
74 struct hvc_struct *hvc;
75
76 /*
77 * This number identifies the number that we used to register
78 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
79 * number passed on by the hvc callbacks to us to
80 * differentiate between the other console ports handled by
81 * this driver
82 */
83 u32 vtermno;
84};
85
fdb9a054
AS
86struct port_buffer {
87 char *buf;
88
89 /* size of the buffer in *buf above */
90 size_t size;
91
92 /* used length of the buffer */
93 size_t len;
94 /* offset in the buf from which to consume data */
95 size_t offset;
96};
97
17634ba2
AS
98/*
99 * This is a per-device struct that stores data common to all the
100 * ports for that device (vdev->priv).
101 */
102struct ports_device {
103 /*
104 * Workqueue handlers where we process deferred work after
105 * notification
106 */
107 struct work_struct control_work;
108
109 struct list_head ports;
110
111 /* To protect the list of ports */
112 spinlock_t ports_lock;
113
114 /* To protect the vq operations for the control channel */
115 spinlock_t cvq_lock;
116
117 /* The current config space is stored here */
118 struct virtio_console_config config;
119
120 /* The virtio device we're associated with */
121 struct virtio_device *vdev;
122
123 /*
124 * A couple of virtqueues for the control channel: one for
125 * guest->host transfers, one for host->guest transfers
126 */
127 struct virtqueue *c_ivq, *c_ovq;
128
129 /* Array of per-port IO virtqueues */
130 struct virtqueue **in_vqs, **out_vqs;
fb08bd27
AS
131
132 /* Used for numbering devices for sysfs and debugfs */
133 unsigned int drv_index;
134
135 /* Major number for this device. Ports will be created as minors. */
136 int chr_major;
17634ba2
AS
137};
138
1c85bf35 139/* This struct holds the per-port data */
21206ede 140struct port {
17634ba2
AS
141 /* Next port in the list, head is in the ports_device */
142 struct list_head list;
143
1c85bf35
AS
144 /* Pointer to the parent virtio_console device */
145 struct ports_device *portdev;
fdb9a054
AS
146
147 /* The current buffer from which data has to be fed to readers */
148 struct port_buffer *inbuf;
21206ede 149
203baab8
AS
150 /*
151 * To protect the operations on the in_vq associated with this
152 * port. Has to be a spinlock because it can be called from
153 * interrupt context (get_char()).
154 */
155 spinlock_t inbuf_lock;
156
1c85bf35
AS
157 /* The IO vqs for this port */
158 struct virtqueue *in_vq, *out_vq;
159
4f23c573
AS
160 /*
161 * The entries in this struct will be valid if this port is
162 * hooked up to an hvc console
163 */
164 struct console cons;
17634ba2 165
fb08bd27
AS
166 /* Each port associates with a separate char device */
167 struct cdev cdev;
168 struct device *dev;
169
2030fa49
AS
170 /* A waitqueue for poll() or blocking read operations */
171 wait_queue_head_t waitqueue;
172
431edb8a
AS
173 /* The 'name' of the port that we expose via sysfs properties */
174 char *name;
175
17634ba2
AS
176 /* The 'id' to identify the port with the Host */
177 u32 id;
2030fa49
AS
178
179 /* Is the host device open */
180 bool host_connected;
3c7969cc
AS
181
182 /* We should allow only one process to open a port */
183 bool guest_connected;
21206ede 184};
31610434 185
971f3390
RR
186/* This is the very early arch-specified put chars function. */
187static int (*early_put_chars)(u32, const char *, int);
188
38edf58d
AS
189static struct port *find_port_by_vtermno(u32 vtermno)
190{
191 struct port *port;
4f23c573 192 struct console *cons;
38edf58d
AS
193 unsigned long flags;
194
195 spin_lock_irqsave(&pdrvdata_lock, flags);
4f23c573
AS
196 list_for_each_entry(cons, &pdrvdata.consoles, list) {
197 if (cons->vtermno == vtermno) {
198 port = container_of(cons, struct port, cons);
38edf58d 199 goto out;
4f23c573 200 }
38edf58d
AS
201 }
202 port = NULL;
203out:
204 spin_unlock_irqrestore(&pdrvdata_lock, flags);
205 return port;
206}
207
17634ba2
AS
208static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
209{
210 struct port *port;
211 unsigned long flags;
212
213 spin_lock_irqsave(&portdev->ports_lock, flags);
214 list_for_each_entry(port, &portdev->ports, list)
215 if (port->id == id)
216 goto out;
217 port = NULL;
218out:
219 spin_unlock_irqrestore(&portdev->ports_lock, flags);
220
221 return port;
222}
223
203baab8
AS
224static struct port *find_port_by_vq(struct ports_device *portdev,
225 struct virtqueue *vq)
226{
227 struct port *port;
203baab8
AS
228 unsigned long flags;
229
17634ba2
AS
230 spin_lock_irqsave(&portdev->ports_lock, flags);
231 list_for_each_entry(port, &portdev->ports, list)
203baab8
AS
232 if (port->in_vq == vq || port->out_vq == vq)
233 goto out;
203baab8
AS
234 port = NULL;
235out:
17634ba2 236 spin_unlock_irqrestore(&portdev->ports_lock, flags);
203baab8
AS
237 return port;
238}
239
17634ba2
AS
240static bool is_console_port(struct port *port)
241{
242 if (port->cons.hvc)
243 return true;
244 return false;
245}
246
247static inline bool use_multiport(struct ports_device *portdev)
248{
249 /*
250 * This condition can be true when put_chars is called from
251 * early_init
252 */
253 if (!portdev->vdev)
254 return 0;
255 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
256}
257
fdb9a054
AS
258static void free_buf(struct port_buffer *buf)
259{
260 kfree(buf->buf);
261 kfree(buf);
262}
263
264static struct port_buffer *alloc_buf(size_t buf_size)
265{
266 struct port_buffer *buf;
267
268 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
269 if (!buf)
270 goto fail;
271 buf->buf = kzalloc(buf_size, GFP_KERNEL);
272 if (!buf->buf)
273 goto free_buf;
274 buf->len = 0;
275 buf->offset = 0;
276 buf->size = buf_size;
277 return buf;
278
279free_buf:
280 kfree(buf);
281fail:
282 return NULL;
283}
284
a3cde449
AS
285/* Callers should take appropriate locks */
286static void *get_inbuf(struct port *port)
287{
288 struct port_buffer *buf;
289 struct virtqueue *vq;
290 unsigned int len;
291
292 vq = port->in_vq;
293 buf = vq->vq_ops->get_buf(vq, &len);
294 if (buf) {
295 buf->len = len;
296 buf->offset = 0;
297 }
298 return buf;
299}
300
e27b5198
AS
301/*
302 * Create a scatter-gather list representing our input buffer and put
303 * it in the queue.
304 *
305 * Callers should take appropriate locks.
306 */
203baab8 307static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
e27b5198
AS
308{
309 struct scatterlist sg[1];
203baab8 310 int ret;
1c85bf35 311
e27b5198
AS
312 sg_init_one(sg, buf->buf, buf->size);
313
203baab8 314 ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
e27b5198 315 vq->vq_ops->kick(vq);
203baab8
AS
316 return ret;
317}
318
319static bool port_has_data(struct port *port)
320{
321 unsigned long flags;
322 bool ret;
323
324 ret = false;
325 spin_lock_irqsave(&port->inbuf_lock, flags);
326 if (port->inbuf)
327 ret = true;
328 spin_unlock_irqrestore(&port->inbuf_lock, flags);
329
330 return ret;
331}
332
17634ba2
AS
333static ssize_t send_control_msg(struct port *port, unsigned int event,
334 unsigned int value)
335{
336 struct scatterlist sg[1];
337 struct virtio_console_control cpkt;
338 struct virtqueue *vq;
339 int len;
340
341 if (!use_multiport(port->portdev))
342 return 0;
343
344 cpkt.id = port->id;
345 cpkt.event = event;
346 cpkt.value = value;
347
348 vq = port->portdev->c_ovq;
349
350 sg_init_one(sg, &cpkt, sizeof(cpkt));
351 if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
352 vq->vq_ops->kick(vq);
353 while (!vq->vq_ops->get_buf(vq, &len))
354 cpu_relax();
355 }
356 return 0;
357}
358
f997f00b
AS
359static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
360{
361 struct scatterlist sg[1];
362 struct virtqueue *out_vq;
363 ssize_t ret;
364 unsigned int len;
365
366 out_vq = port->out_vq;
367
368 sg_init_one(sg, in_buf, in_count);
369 ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
370
371 /* Tell Host to go! */
372 out_vq->vq_ops->kick(out_vq);
373
374 if (ret < 0) {
375 len = 0;
376 goto fail;
377 }
378
379 /*
380 * Wait till the host acknowledges it pushed out the data we
381 * sent. Also ensure we return to userspace the number of
382 * bytes that were successfully consumed by the host.
383 */
384 while (!out_vq->vq_ops->get_buf(out_vq, &len))
385 cpu_relax();
386fail:
387 /* We're expected to return the amount of data we wrote */
388 return len;
389}
390
203baab8
AS
391/*
392 * Give out the data that's requested from the buffer that we have
393 * queued up.
394 */
b766ceed
AS
395static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
396 bool to_user)
203baab8
AS
397{
398 struct port_buffer *buf;
399 unsigned long flags;
400
401 if (!out_count || !port_has_data(port))
402 return 0;
403
404 buf = port->inbuf;
b766ceed 405 out_count = min(out_count, buf->len - buf->offset);
203baab8 406
b766ceed
AS
407 if (to_user) {
408 ssize_t ret;
409
410 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
411 if (ret)
412 return -EFAULT;
413 } else {
414 memcpy(out_buf, buf->buf + buf->offset, out_count);
415 }
203baab8 416
203baab8
AS
417 buf->offset += out_count;
418
419 if (buf->offset == buf->len) {
420 /*
421 * We're done using all the data in this buffer.
422 * Re-queue so that the Host can send us more data.
423 */
424 spin_lock_irqsave(&port->inbuf_lock, flags);
425 port->inbuf = NULL;
426
427 if (add_inbuf(port->in_vq, buf) < 0)
fb08bd27 428 dev_warn(port->dev, "failed add_buf\n");
203baab8
AS
429
430 spin_unlock_irqrestore(&port->inbuf_lock, flags);
431 }
b766ceed 432 /* Return the number of bytes actually copied */
203baab8 433 return out_count;
e27b5198
AS
434}
435
2030fa49
AS
436/* The condition that must be true for polling to end */
437static bool wait_is_over(struct port *port)
438{
439 return port_has_data(port) || !port->host_connected;
440}
441
442static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
443 size_t count, loff_t *offp)
444{
445 struct port *port;
446 ssize_t ret;
447
448 port = filp->private_data;
449
450 if (!port_has_data(port)) {
451 /*
452 * If nothing's connected on the host just return 0 in
453 * case of list_empty; this tells the userspace app
454 * that there's no connection
455 */
456 if (!port->host_connected)
457 return 0;
458 if (filp->f_flags & O_NONBLOCK)
459 return -EAGAIN;
460
461 ret = wait_event_interruptible(port->waitqueue,
462 wait_is_over(port));
463 if (ret < 0)
464 return ret;
465 }
466 /*
467 * We could've received a disconnection message while we were
468 * waiting for more data.
469 *
470 * This check is not clubbed in the if() statement above as we
471 * might receive some data as well as the host could get
472 * disconnected after we got woken up from our wait. So we
473 * really want to give off whatever data we have and only then
474 * check for host_connected.
475 */
476 if (!port_has_data(port) && !port->host_connected)
477 return 0;
478
479 return fill_readbuf(port, ubuf, count, true);
480}
481
482static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
483 size_t count, loff_t *offp)
484{
485 struct port *port;
486 char *buf;
487 ssize_t ret;
488
489 port = filp->private_data;
490
491 count = min((size_t)(32 * 1024), count);
492
493 buf = kmalloc(count, GFP_KERNEL);
494 if (!buf)
495 return -ENOMEM;
496
497 ret = copy_from_user(buf, ubuf, count);
498 if (ret) {
499 ret = -EFAULT;
500 goto free_buf;
501 }
502
503 ret = send_buf(port, buf, count);
504free_buf:
505 kfree(buf);
506 return ret;
507}
508
509static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
510{
511 struct port *port;
512 unsigned int ret;
513
514 port = filp->private_data;
515 poll_wait(filp, &port->waitqueue, wait);
516
517 ret = 0;
518 if (port->inbuf)
519 ret |= POLLIN | POLLRDNORM;
520 if (port->host_connected)
521 ret |= POLLOUT;
522 if (!port->host_connected)
523 ret |= POLLHUP;
524
525 return ret;
526}
527
528static int port_fops_release(struct inode *inode, struct file *filp)
529{
530 struct port *port;
531
532 port = filp->private_data;
533
534 /* Notify host of port being closed */
535 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
536
3c7969cc
AS
537 port->guest_connected = false;
538
2030fa49
AS
539 return 0;
540}
541
542static int port_fops_open(struct inode *inode, struct file *filp)
543{
544 struct cdev *cdev = inode->i_cdev;
545 struct port *port;
546
547 port = container_of(cdev, struct port, cdev);
548 filp->private_data = port;
549
550 /*
551 * Don't allow opening of console port devices -- that's done
552 * via /dev/hvc
553 */
554 if (is_console_port(port))
555 return -ENXIO;
556
3c7969cc
AS
557 /* Allow only one process to open a particular port at a time */
558 spin_lock_irq(&port->inbuf_lock);
559 if (port->guest_connected) {
560 spin_unlock_irq(&port->inbuf_lock);
561 return -EMFILE;
562 }
563
564 port->guest_connected = true;
565 spin_unlock_irq(&port->inbuf_lock);
566
2030fa49
AS
567 /* Notify host of port being opened */
568 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
569
570 return 0;
571}
572
573/*
574 * The file operations that we support: programs in the guest can open
575 * a console device, read from it, write to it, poll for data and
576 * close it. The devices are at
577 * /dev/vport<device number>p<port number>
578 */
579static const struct file_operations port_fops = {
580 .owner = THIS_MODULE,
581 .open = port_fops_open,
582 .read = port_fops_read,
583 .write = port_fops_write,
584 .poll = port_fops_poll,
585 .release = port_fops_release,
586};
587
a23ea924
RR
588/*
589 * The put_chars() callback is pretty straightforward.
31610434 590 *
a23ea924
RR
591 * We turn the characters into a scatter-gather list, add it to the
592 * output queue and then kick the Host. Then we sit here waiting for
593 * it to finish: inefficient in theory, but in practice
594 * implementations will do it immediately (lguest's Launcher does).
595 */
31610434
RR
596static int put_chars(u32 vtermno, const char *buf, int count)
597{
21206ede 598 struct port *port;
38edf58d
AS
599
600 port = find_port_by_vtermno(vtermno);
601 if (!port)
602 return 0;
31610434 603
971f3390
RR
604 if (unlikely(early_put_chars))
605 return early_put_chars(vtermno, buf, count);
606
f997f00b 607 return send_buf(port, (void *)buf, count);
31610434
RR
608}
609
a23ea924
RR
610/*
611 * get_chars() is the callback from the hvc_console infrastructure
612 * when an interrupt is received.
31610434 613 *
203baab8
AS
614 * We call out to fill_readbuf that gets us the required data from the
615 * buffers that are queued up.
a23ea924 616 */
31610434
RR
617static int get_chars(u32 vtermno, char *buf, int count)
618{
21206ede
RR
619 struct port *port;
620
38edf58d
AS
621 port = find_port_by_vtermno(vtermno);
622 if (!port)
623 return 0;
21206ede 624
31610434 625 /* If we don't have an input queue yet, we can't get input. */
21206ede 626 BUG_ON(!port->in_vq);
31610434 627
b766ceed 628 return fill_readbuf(port, buf, count, false);
31610434 629}
31610434 630
cb06e367 631static void resize_console(struct port *port)
c2983458 632{
cb06e367 633 struct virtio_device *vdev;
c2983458
CB
634 struct winsize ws;
635
cb06e367
AS
636 vdev = port->portdev->vdev;
637 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
638 vdev->config->get(vdev,
639 offsetof(struct virtio_console_config, cols),
640 &ws.ws_col, sizeof(u16));
641 vdev->config->get(vdev,
642 offsetof(struct virtio_console_config, rows),
643 &ws.ws_row, sizeof(u16));
4f23c573 644 hvc_resize(port->cons.hvc, ws);
c2983458
CB
645 }
646}
647
cb06e367
AS
648static void virtcons_apply_config(struct virtio_device *vdev)
649{
650 resize_console(find_port_by_vtermno(0));
651}
652
38edf58d 653/* We set the configuration at this point, since we now have a tty */
91fcad19
CB
654static int notifier_add_vio(struct hvc_struct *hp, int data)
655{
38edf58d
AS
656 struct port *port;
657
658 port = find_port_by_vtermno(hp->vtermno);
659 if (!port)
660 return -EINVAL;
661
91fcad19 662 hp->irq_requested = 1;
cb06e367 663 resize_console(port);
c2983458 664
91fcad19
CB
665 return 0;
666}
667
668static void notifier_del_vio(struct hvc_struct *hp, int data)
669{
670 hp->irq_requested = 0;
671}
672
17634ba2 673/* The operations for console ports. */
1dff3996 674static const struct hv_ops hv_ops = {
971f3390
RR
675 .get_chars = get_chars,
676 .put_chars = put_chars,
677 .notifier_add = notifier_add_vio,
678 .notifier_del = notifier_del_vio,
679 .notifier_hangup = notifier_del_vio,
680};
681
682/*
683 * Console drivers are initialized very early so boot messages can go
684 * out, so we do things slightly differently from the generic virtio
685 * initialization of the net and block drivers.
686 *
687 * At this stage, the console is output-only. It's too early to set
688 * up a virtqueue, so we let the drivers do some boutique early-output
689 * thing.
690 */
691int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
692{
693 early_put_chars = put_chars;
694 return hvc_instantiate(0, 0, &hv_ops);
695}
696
17634ba2 697int init_port_console(struct port *port)
cfa6d379
AS
698{
699 int ret;
700
701 /*
702 * The Host's telling us this port is a console port. Hook it
703 * up with an hvc console.
704 *
705 * To set up and manage our virtual console, we call
706 * hvc_alloc().
707 *
708 * The first argument of hvc_alloc() is the virtual console
709 * number. The second argument is the parameter for the
710 * notification mechanism (like irq number). We currently
711 * leave this as zero, virtqueues have implicit notifications.
712 *
713 * The third argument is a "struct hv_ops" containing the
714 * put_chars() get_chars(), notifier_add() and notifier_del()
715 * pointers. The final argument is the output buffer size: we
716 * can do any size, so we put PAGE_SIZE here.
717 */
718 port->cons.vtermno = pdrvdata.next_vtermno;
719
720 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
721 if (IS_ERR(port->cons.hvc)) {
722 ret = PTR_ERR(port->cons.hvc);
723 port->cons.hvc = NULL;
724 return ret;
725 }
726 spin_lock_irq(&pdrvdata_lock);
727 pdrvdata.next_vtermno++;
728 list_add_tail(&port->cons.list, &pdrvdata.consoles);
729 spin_unlock_irq(&pdrvdata_lock);
3c7969cc 730 port->guest_connected = true;
cfa6d379 731
2030fa49
AS
732 /* Notify host of port being opened */
733 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
734
cfa6d379
AS
735 return 0;
736}
737
431edb8a
AS
738static ssize_t show_port_name(struct device *dev,
739 struct device_attribute *attr, char *buffer)
740{
741 struct port *port;
742
743 port = dev_get_drvdata(dev);
744
745 return sprintf(buffer, "%s\n", port->name);
746}
747
748static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
749
750static struct attribute *port_sysfs_entries[] = {
751 &dev_attr_name.attr,
752 NULL
753};
754
755static struct attribute_group port_attribute_group = {
756 .name = NULL, /* put in device directory */
757 .attrs = port_sysfs_entries,
758};
759
17634ba2
AS
760/* Any private messages that the Host and Guest want to share */
761static void handle_control_message(struct ports_device *portdev,
762 struct port_buffer *buf)
763{
764 struct virtio_console_control *cpkt;
765 struct port *port;
431edb8a
AS
766 size_t name_size;
767 int err;
17634ba2
AS
768
769 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
770
771 port = find_port_by_id(portdev, cpkt->id);
772 if (!port) {
773 /* No valid header at start of buffer. Drop it. */
774 dev_dbg(&portdev->vdev->dev,
775 "Invalid index %u in control packet\n", cpkt->id);
776 return;
777 }
778
779 switch (cpkt->event) {
780 case VIRTIO_CONSOLE_CONSOLE_PORT:
781 if (!cpkt->value)
782 break;
783 if (is_console_port(port))
784 break;
785
786 init_port_console(port);
787 /*
788 * Could remove the port here in case init fails - but
789 * have to notify the host first.
790 */
791 break;
792 case VIRTIO_CONSOLE_RESIZE:
793 if (!is_console_port(port))
794 break;
795 port->cons.hvc->irq_requested = 1;
796 resize_console(port);
797 break;
2030fa49
AS
798 case VIRTIO_CONSOLE_PORT_OPEN:
799 port->host_connected = cpkt->value;
800 wake_up_interruptible(&port->waitqueue);
801 break;
431edb8a
AS
802 case VIRTIO_CONSOLE_PORT_NAME:
803 /*
804 * Skip the size of the header and the cpkt to get the size
805 * of the name that was sent
806 */
807 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
808
809 port->name = kmalloc(name_size, GFP_KERNEL);
810 if (!port->name) {
811 dev_err(port->dev,
812 "Not enough space to store port name\n");
813 break;
814 }
815 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
816 name_size - 1);
817 port->name[name_size - 1] = 0;
818
819 /*
820 * Since we only have one sysfs attribute, 'name',
821 * create it only if we have a name for the port.
822 */
823 err = sysfs_create_group(&port->dev->kobj,
824 &port_attribute_group);
825 if (err)
826 dev_err(port->dev,
827 "Error %d creating sysfs device attributes\n",
828 err);
829
830 break;
17634ba2
AS
831 }
832}
833
834static void control_work_handler(struct work_struct *work)
835{
836 struct ports_device *portdev;
837 struct virtqueue *vq;
838 struct port_buffer *buf;
839 unsigned int len;
840
841 portdev = container_of(work, struct ports_device, control_work);
842 vq = portdev->c_ivq;
843
844 spin_lock(&portdev->cvq_lock);
845 while ((buf = vq->vq_ops->get_buf(vq, &len))) {
846 spin_unlock(&portdev->cvq_lock);
847
848 buf->len = len;
849 buf->offset = 0;
850
851 handle_control_message(portdev, buf);
852
853 spin_lock(&portdev->cvq_lock);
854 if (add_inbuf(portdev->c_ivq, buf) < 0) {
855 dev_warn(&portdev->vdev->dev,
856 "Error adding buffer to queue\n");
857 free_buf(buf);
858 }
859 }
860 spin_unlock(&portdev->cvq_lock);
861}
862
863static void in_intr(struct virtqueue *vq)
864{
865 struct port *port;
866 unsigned long flags;
867
868 port = find_port_by_vq(vq->vdev->priv, vq);
869 if (!port)
870 return;
871
872 spin_lock_irqsave(&port->inbuf_lock, flags);
873 port->inbuf = get_inbuf(port);
874
875 spin_unlock_irqrestore(&port->inbuf_lock, flags);
876
2030fa49
AS
877 wake_up_interruptible(&port->waitqueue);
878
17634ba2
AS
879 if (is_console_port(port) && hvc_poll(port->cons.hvc))
880 hvc_kick();
881}
882
883static void control_intr(struct virtqueue *vq)
884{
885 struct ports_device *portdev;
886
887 portdev = vq->vdev->priv;
888 schedule_work(&portdev->control_work);
889}
890
891static void fill_queue(struct virtqueue *vq, spinlock_t *lock)
892{
893 struct port_buffer *buf;
894 int ret;
895
896 do {
897 buf = alloc_buf(PAGE_SIZE);
898 if (!buf)
899 break;
900
901 spin_lock_irq(lock);
902 ret = add_inbuf(vq, buf);
903 if (ret < 0) {
904 spin_unlock_irq(lock);
905 free_buf(buf);
906 break;
907 }
908 spin_unlock_irq(lock);
909 } while (ret > 0);
910}
911
912static int add_port(struct ports_device *portdev, u32 id)
d8a02bd5 913{
21206ede 914 struct port *port;
203baab8 915 struct port_buffer *inbuf;
fb08bd27 916 dev_t devt;
31610434 917 int err;
31610434 918
1c85bf35 919 port = kmalloc(sizeof(*port), GFP_KERNEL);
d8a02bd5
RR
920 if (!port) {
921 err = -ENOMEM;
922 goto fail;
f550804a 923 }
73954488 924
1c85bf35 925 port->portdev = portdev;
17634ba2 926 port->id = id;
203baab8 927
431edb8a 928 port->name = NULL;
203baab8 929 port->inbuf = NULL;
17634ba2 930 port->cons.hvc = NULL;
203baab8 931
3c7969cc 932 port->host_connected = port->guest_connected = false;
2030fa49 933
17634ba2
AS
934 port->in_vq = portdev->in_vqs[port->id];
935 port->out_vq = portdev->out_vqs[port->id];
31610434 936
2030fa49 937 cdev_init(&port->cdev, &port_fops);
fb08bd27
AS
938
939 devt = MKDEV(portdev->chr_major, id);
940 err = cdev_add(&port->cdev, devt, 1);
941 if (err < 0) {
942 dev_err(&port->portdev->vdev->dev,
943 "Error %d adding cdev for port %u\n", err, id);
944 goto free_port;
945 }
946 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
947 devt, port, "vport%up%u",
948 port->portdev->drv_index, id);
949 if (IS_ERR(port->dev)) {
950 err = PTR_ERR(port->dev);
951 dev_err(&port->portdev->vdev->dev,
952 "Error %d creating device for port %u\n",
953 err, id);
954 goto free_cdev;
955 }
956
203baab8 957 spin_lock_init(&port->inbuf_lock);
2030fa49 958 init_waitqueue_head(&port->waitqueue);
203baab8
AS
959
960 inbuf = alloc_buf(PAGE_SIZE);
961 if (!inbuf) {
1c85bf35 962 err = -ENOMEM;
fb08bd27 963 goto free_device;
1c85bf35 964 }
31610434 965
203baab8
AS
966 /* Register the input buffer the first time. */
967 add_inbuf(port->in_vq, inbuf);
968
17634ba2
AS
969 /*
970 * If we're not using multiport support, this has to be a console port
971 */
972 if (!use_multiport(port->portdev)) {
973 err = init_port_console(port);
974 if (err)
975 goto free_inbuf;
976 }
977
978 spin_lock_irq(&portdev->ports_lock);
979 list_add_tail(&port->list, &port->portdev->ports);
980 spin_unlock_irq(&portdev->ports_lock);
981
982 /*
983 * Tell the Host we're set so that it can send us various
984 * configuration parameters for this port (eg, port name,
985 * caching, whether this is a console port, etc.)
986 */
987 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
38edf58d 988
1c85bf35
AS
989 return 0;
990
991free_inbuf:
203baab8 992 free_buf(inbuf);
fb08bd27
AS
993free_device:
994 device_destroy(pdrvdata.class, port->dev->devt);
995free_cdev:
996 cdev_del(&port->cdev);
1c85bf35
AS
997free_port:
998 kfree(port);
999fail:
1000 return err;
1001}
1002
2658a79a
AS
1003static int init_vqs(struct ports_device *portdev)
1004{
1005 vq_callback_t **io_callbacks;
1006 char **io_names;
1007 struct virtqueue **vqs;
17634ba2 1008 u32 i, j, nr_ports, nr_queues;
2658a79a
AS
1009 int err;
1010
17634ba2
AS
1011 nr_ports = portdev->config.max_nr_ports;
1012 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
2658a79a
AS
1013
1014 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1015 if (!vqs) {
1016 err = -ENOMEM;
1017 goto fail;
1018 }
1019 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1020 if (!io_callbacks) {
1021 err = -ENOMEM;
1022 goto free_vqs;
1023 }
1024 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1025 if (!io_names) {
1026 err = -ENOMEM;
1027 goto free_callbacks;
1028 }
1029 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1030 GFP_KERNEL);
1031 if (!portdev->in_vqs) {
1032 err = -ENOMEM;
1033 goto free_names;
1034 }
1035 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1036 GFP_KERNEL);
1037 if (!portdev->out_vqs) {
1038 err = -ENOMEM;
1039 goto free_invqs;
1040 }
1041
17634ba2
AS
1042 /*
1043 * For backward compat (newer host but older guest), the host
1044 * spawns a console port first and also inits the vqs for port
1045 * 0 before others.
1046 */
1047 j = 0;
1048 io_callbacks[j] = in_intr;
1049 io_callbacks[j + 1] = NULL;
1050 io_names[j] = "input";
1051 io_names[j + 1] = "output";
1052 j += 2;
1053
1054 if (use_multiport(portdev)) {
1055 io_callbacks[j] = control_intr;
1056 io_callbacks[j + 1] = NULL;
1057 io_names[j] = "control-i";
1058 io_names[j + 1] = "control-o";
1059
1060 for (i = 1; i < nr_ports; i++) {
1061 j += 2;
1062 io_callbacks[j] = in_intr;
1063 io_callbacks[j + 1] = NULL;
1064 io_names[j] = "input";
1065 io_names[j + 1] = "output";
1066 }
1067 }
2658a79a
AS
1068 /* Find the queues. */
1069 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1070 io_callbacks,
1071 (const char **)io_names);
1072 if (err)
1073 goto free_outvqs;
1074
17634ba2 1075 j = 0;
2658a79a
AS
1076 portdev->in_vqs[0] = vqs[0];
1077 portdev->out_vqs[0] = vqs[1];
17634ba2
AS
1078 j += 2;
1079 if (use_multiport(portdev)) {
1080 portdev->c_ivq = vqs[j];
1081 portdev->c_ovq = vqs[j + 1];
1082
1083 for (i = 1; i < nr_ports; i++) {
1084 j += 2;
1085 portdev->in_vqs[i] = vqs[j];
1086 portdev->out_vqs[i] = vqs[j + 1];
1087 }
1088 }
2658a79a
AS
1089 kfree(io_callbacks);
1090 kfree(io_names);
1091 kfree(vqs);
1092
1093 return 0;
1094
1095free_names:
1096 kfree(io_names);
1097free_callbacks:
1098 kfree(io_callbacks);
1099free_outvqs:
1100 kfree(portdev->out_vqs);
1101free_invqs:
1102 kfree(portdev->in_vqs);
1103free_vqs:
1104 kfree(vqs);
1105fail:
1106 return err;
1107}
1108
fb08bd27
AS
1109static const struct file_operations portdev_fops = {
1110 .owner = THIS_MODULE,
1111};
1112
1c85bf35
AS
1113/*
1114 * Once we're further in boot, we get probed like any other virtio
1115 * device.
17634ba2
AS
1116 *
1117 * If the host also supports multiple console ports, we check the
1118 * config space to see how many ports the host has spawned. We
1119 * initialize each port found.
1c85bf35
AS
1120 */
1121static int __devinit virtcons_probe(struct virtio_device *vdev)
1122{
1c85bf35 1123 struct ports_device *portdev;
17634ba2 1124 u32 i;
1c85bf35 1125 int err;
17634ba2 1126 bool multiport;
1c85bf35
AS
1127
1128 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1129 if (!portdev) {
1130 err = -ENOMEM;
1131 goto fail;
1132 }
1133
1134 /* Attach this portdev to this virtio_device, and vice-versa. */
1135 portdev->vdev = vdev;
1136 vdev->priv = portdev;
1137
fb08bd27
AS
1138 spin_lock_irq(&pdrvdata_lock);
1139 portdev->drv_index = pdrvdata.index++;
1140 spin_unlock_irq(&pdrvdata_lock);
1141
1142 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1143 &portdev_fops);
1144 if (portdev->chr_major < 0) {
1145 dev_err(&vdev->dev,
1146 "Error %d registering chrdev for device %u\n",
1147 portdev->chr_major, portdev->drv_index);
1148 err = portdev->chr_major;
1149 goto free;
1150 }
1151
17634ba2
AS
1152 multiport = false;
1153 portdev->config.nr_ports = 1;
1154 portdev->config.max_nr_ports = 1;
1155 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1156 multiport = true;
1157 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1158
1159 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1160 nr_ports),
1161 &portdev->config.nr_ports,
1162 sizeof(portdev->config.nr_ports));
1163 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1164 max_nr_ports),
1165 &portdev->config.max_nr_ports,
1166 sizeof(portdev->config.max_nr_ports));
1167 if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
1168 dev_warn(&vdev->dev,
1169 "More ports (%u) specified than allowed (%u). Will init %u ports.",
1170 portdev->config.nr_ports,
1171 portdev->config.max_nr_ports,
1172 portdev->config.max_nr_ports);
1173
1174 portdev->config.nr_ports = portdev->config.max_nr_ports;
1175 }
1176 }
1177
1178 /* Let the Host know we support multiple ports.*/
1179 vdev->config->finalize_features(vdev);
1180
2658a79a
AS
1181 err = init_vqs(portdev);
1182 if (err < 0) {
1183 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
fb08bd27 1184 goto free_chrdev;
2658a79a 1185 }
1c85bf35 1186
17634ba2
AS
1187 spin_lock_init(&portdev->ports_lock);
1188 INIT_LIST_HEAD(&portdev->ports);
1189
1190 if (multiport) {
1191 spin_lock_init(&portdev->cvq_lock);
1192 INIT_WORK(&portdev->control_work, &control_work_handler);
1193
1194 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1195 }
1196
1197 for (i = 0; i < portdev->config.nr_ports; i++)
1198 add_port(portdev, i);
1c85bf35 1199
971f3390
RR
1200 /* Start using the new console output. */
1201 early_put_chars = NULL;
31610434
RR
1202 return 0;
1203
fb08bd27
AS
1204free_chrdev:
1205 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
31610434 1206free:
1c85bf35 1207 kfree(portdev);
31610434
RR
1208fail:
1209 return err;
1210}
1211
1212static struct virtio_device_id id_table[] = {
1213 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1214 { 0 },
1215};
1216
c2983458
CB
1217static unsigned int features[] = {
1218 VIRTIO_CONSOLE_F_SIZE,
17634ba2 1219 VIRTIO_CONSOLE_F_MULTIPORT,
c2983458
CB
1220};
1221
31610434 1222static struct virtio_driver virtio_console = {
c2983458
CB
1223 .feature_table = features,
1224 .feature_table_size = ARRAY_SIZE(features),
31610434
RR
1225 .driver.name = KBUILD_MODNAME,
1226 .driver.owner = THIS_MODULE,
1227 .id_table = id_table,
1228 .probe = virtcons_probe,
c2983458 1229 .config_changed = virtcons_apply_config,
31610434
RR
1230};
1231
1232static int __init init(void)
1233{
fb08bd27
AS
1234 int err;
1235
1236 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1237 if (IS_ERR(pdrvdata.class)) {
1238 err = PTR_ERR(pdrvdata.class);
1239 pr_err("Error %d creating virtio-ports class\n", err);
1240 return err;
1241 }
38edf58d
AS
1242 INIT_LIST_HEAD(&pdrvdata.consoles);
1243
31610434
RR
1244 return register_virtio_driver(&virtio_console);
1245}
1246module_init(init);
1247
1248MODULE_DEVICE_TABLE(virtio, id_table);
1249MODULE_DESCRIPTION("Virtio console driver");
1250MODULE_LICENSE("GPL");
This page took 0.404615 seconds and 5 git commands to generate.